package appnode;

import javax.jdo.PersistenceManager;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import java.util.List;
import java.util.Date;
import java.util.ArrayList;
import appnode.Node;
import appnode.Link;
import appnode.Interface;
import appnode.Ratings;
import javax.jdo.Query;
import java.lang.*;


public class SearchNode {
	
	public static Node getByName(String name) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Node.class);
		query.setFilter("nodename==nodenameParam");
		query.declareParameters("String nodenameParam");
		List<Node> results = (List<Node>) query.execute(name);
		return results.get(0);
	}
	
	public static List<Node> getByOwner(String name) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Node.class);
		query.setFilter("owner==nameParam");
		query.declareParameters("String nameParam");
		List<Node> results = (List<Node>) query.execute(name);
		return results;
	}
	
	
	public static String validatePage(String name, String owner) {
		
		String result = "no";
		int j = 0;
		for (int i=0;i<getByOwner(owner).size();i++) {
			if (name.equals(getByOwner(owner).get(i).getNodename())) {
				j++;
			}
		}
		if (j!=0) {
			result = "yes";
		}
		return result;
	}

	public static String getRatings(String name) {
		
		String result = "notnull";
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Ratings.class);
		query.setFilter("owner==nameParam");
		query.declareParameters("String nameParam");
		List<Ratings> results = (List<Ratings>) query.execute(name);
		if (results.isEmpty()) {
			result = "null";
		}
		return result;
	}
	
	public static List<Node> getLinks(Node node) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Node> nodelist = new ArrayList<Node>();
		Key key;
		Interface if1;
		
		for (int i=0;i<node.getInterfaces().size();i++) {
			if (node.getInterfaces().get(i).getLink()!=null && !node.getInterfaces().get(i).getLink().equals("")) {
				key = KeyFactory.stringToKey(node.getInterfaces().get(i).getFrominterface());
				if1 = pm.getObjectById(Interface.class,key);
				if (if1.getFrominterface().equals(node.getInterfaces().get(i).getKeyStr())) {
					nodelist.add(if1.getNode());
				} //if
			} // if
		} // for
		return nodelist;
	}
	
	public static List<Interface> getLinklist(Node node) {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Interface> linklist = new ArrayList<Interface>();
		Key key;
		Interface if1;
	
		
		for (int i=0;i<node.getInterfaces().size();i++) {
			if (node.getInterfaces().get(i).getLink()!=null && !node.getInterfaces().get(i).getLink().equals("")) {
				key = KeyFactory.stringToKey(node.getInterfaces().get(i).getFrominterface());
				if1 = pm.getObjectById(Interface.class,key);
				if (if1.getFrominterface().equals(node.getInterfaces().get(i).getKeyStr())) {
					linklist.add(if1);
				} //if
			} // if
		} // for
		return linklist;
	}
	
	 
	
	public static List<Node> getFreeNodes(String name){
		List<Node> nodelist = new ArrayList<Node>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Node.class);
		query.setFilter("nodename!=nodenameParam");
		query.declareParameters("String nodenameParam");
		List<Node> results = (List<Node>) query.execute(name);
		for (Node n : results) {
			int sizelink = SearchNode.getLinklist(n).size();
			int sizeif = n.getInterfaces().size();
			int sizefree = sizeif - sizelink;
			if (sizefree>0) {
				nodelist.add(n);
			} // if
		} // for
		return nodelist;
	}
	
	
	
	public static List<Node> getNodes(String name){
		List<Node> node = new ArrayList<Node>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Node.class);
		query.setFilter("nodename!=nodenameParam");
		query.declareParameters("String nodenameParam");
		List<Node> results = (List<Node>) query.execute(name);
		
		for (Node n : results) {
			node.add(n);
		}
		return node;		
	}
	
	public static List<Node> sortRatings(String name, String rate) {
		
		List<Node> node = new ArrayList<Node>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Ratings.class);
		query.declareParameters("String nameParam");
		query.setOrdering(rate +" desc");
		List<Ratings> results = (List<Ratings>) query.execute(name);
		for  (Ratings r : results) {
			if (!r.getOwner().equals(name)) {
				for (int i=0;i<getByOwner(r.getOwner()).size();i++) {
					node.add(getByOwner(r.getOwner()).get(i));
				}
			}
		}
		return node;
	}
	
	public static List<Node> getSortRatings(String name, String rate) {
		
		String owner = getByName(name).getOwner();
		List<Node> sortnode = sortRatings(owner,rate);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Node.class);
		query.setFilter("nodename!=nameParam");
		query.declareParameters("String nameParam");
		List<Node> results = (List<Node>) query.execute(name);
		for (Node n : results) {
			if (getRatings(n.getOwner()).equals("null")) {
				sortnode.add(n);
			}
		}
	return sortnode;
	}
	
 	public static List<Node> getByRatings(String name,String rate){
	
		List<Node> node = new ArrayList<Node>();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(NodeRating.class);
		query.setOrdering(rate +" desc");
		List<NodeRating> results = (List<NodeRating>) query.execute();
		for (NodeRating nr : results){
			if (nr.getNode().getNodename().equals(name)) {
				;
			}
			else {
				node.add(nr.getNode());
			}
		}
		return node;
	}
		
	public static List<Node> freeSortRatings(String name, List<Node> node){
		
			List<Node> freesortnode = new ArrayList<Node>();
			for (int i=0;i<node.size();i++){
			int sizelink = SearchNode.getLinklist(node.get(i)).size();
			int sizeif = node.get(i).getInterfaces().size();
			int sizefree = sizeif - sizelink;
				
				if (sizefree>0){
					freesortnode.add(node.get(i));
				}
			}
	return freesortnode;
	}
	
	public static List<Node> getMapNodes(String name, String choice,String rate) {
		
		List <Node> node = new ArrayList<Node>();
		if (choice.equals("freenorate")) {
			node = getFreeNodes(name);
		}
		else if (choice.equals("allnorate")) {
			node = getNodes(name);
		}
		else if (choice.equals("freerate")) {
			node = freeSortRatings(name,getSortRatings(name,rate));
		}
		else if (choice.equals("allrate")) {
			node = getSortRatings(name,rate);
		}
		return node;
	}
}